
const { MarketingModel } = require('../../../../utils/marketing-model.js');
const marketingModel = new MarketingModel();
const app = getApp()
let inter = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 2,
    reqCount:0,
    shopId: '',
    orderCode: ''
  },
  goback () {
    setTimeout(() => {
      wx.reLaunch({
        url: '/pages/index/index'
      })
    },3000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '支付成功'
    })
    if (options.type == 0) {
      this.setData({
        type: 0
      })
      this.goback()
      return
    }
    this.setData({
      shopId: options.shopId,
      orderCode: options.orderCode
    })
    inter = setInterval(() => {
      if (this.data.reqCount == 5) {
        clearInterval(inter)
        wx.showToast({
          icon: 'none',
          title: '投币失败',
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        },1000)
      } else {
        let reqCount = this.data.reqCount + 1
        this.setData({
          reqCount
        })
        this.payComfirm()
      }
    }, 3000)
  },
  payComfirm () {
    marketingModel.machinePayConfirm({
      merchantId: wx.getStorageSync('merchantId'),
      shopId: this.data.shopId,
      orderCode: this.data.orderCode
    }).then(data => {
      // 0：投币成功  1：机台被占用  2：投币中
      this.setData({
        type:data.code
      })
      if (data.code!=2){
        clearInterval(inter)
      }
      if (data.code==0) {
        this.goback()
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})
